package org.snackframework.util;

import java.io.File;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;

public class PathUtils
{
	/**
	 * path format : org.snack.test (package name)
	 * @param servletContext
	 * @param path
	 * @return
	 */
	public static String getClassPath(ServletContext servletContext,String pack)
	{
		StringBuffer buffer=new StringBuffer(getContextAbsolutePath(servletContext));
		
		if(StringUtils.isNotBlank(pack))
		{
			buffer.append("/WEB-INF/classes/"+StringUtils.replaceChars(pack, '.', '/'));
		}
		
		return buffer.toString();
	}

	public static String getContextAbsolutePath(ServletContext servletContext)
	{
		return StringUtils.replaceChars(servletContext.getRealPath("/"), '\\', '/');
	}
	
	public static String getFilePath(File file)
	{
		String path=file.getPath();
		return StringUtils.replaceChars(path, '\\', '/');
	}
	
	/**
	 * 
	 * 当 resource 放到 classpath 下面的时候       getResourcePath(servletContext,package,resourceName)
	 * 当 resource 放到 root classpath 下面的时候  getResourcePath(servletContext,"",resourceName)
	 * 当 resource 放到 web 目录下面的时候 	        getResourcePath(servletContext,null,/path/resourceName)
	 * 
	 * @param servletContext
	 * @param pack
	 * @param relativePath
	 * @return
	 */
	public static String getResourcePath(ServletContext servletContext,String pack,String relativePath)
	{
		StringBuffer buffer=new StringBuffer(getContextAbsolutePath(servletContext));
		if(null!=pack)
		{
			if(""==pack)
				buffer.append("/WEB-INF/classes/");
			else
				buffer.append("/WEB-INF/classes/"+StringUtils.replaceChars(pack, '.', '/'));
		}
		if(StringUtils.isNotBlank(relativePath))
		{
			buffer.append(relativePath);
		}
		return buffer.toString();
	}
	
	public static String getJarPath(ServletContext servletContext,String jarFile)
	{
		StringBuffer buffer=new StringBuffer(getContextAbsolutePath(servletContext));
		buffer.append("/WEB-INF/lib/"+jarFile);
		return buffer.toString();
	}
	
	/**
	 * 获取 获取视图文件（vm，模板）所在的目录文件,如下所示各种可能的写法：
	 * /template/hello.vm 
	 * template/hello.vm
	 * /hello.vm
	 * hello.vm
	 * /template/hello
	 * template/hello
	 * 
	 * @param view
	 * @return
	 */
	public static String getTemplateDirectory(String view)
	{
		if(view.contains("/"))
		{
			String[] seg=view.split("/");
			StringBuffer buffer=new StringBuffer();
			for(int i=0;i<seg.length-1;i++)
			{
				buffer.append(seg[i]+'/');
			}
			String result=buffer.toString();
			if(!StringUtils.startsWith(result, "/") && result.length()>1)
				result="/"+result;
			if(StringUtils.endsWith(result, "/") && result.length()>1)
				result=StringUtils.removeEnd(result, "/");
			return result;
		}
		else
			return "/";
	}
	
	/**
	 * 
	 * 得到模板名称
	 * 
	 * @param view
	 * @return
	 */
	public static String getTemplateName(String view)
	{
		if(view.contains("/"))
		{
			String[] seg=view.split("/");
			return seg[seg.length-1];
		}
		else
			return view;
	}
	
	public static String lowerCaseFirstCharacter(String str)
	{
		// 省略 str 为null和 "" 的判断
		String sub=str.substring(0, 1);
		StringBuffer buffer=new StringBuffer(sub.toLowerCase());
		return buffer.append(str.substring(1)).toString();
	}
	
	public static void main(String[] args)
	{
//		System.out.println(getTemplateDirectory("template/hello"));
		System.out.println(getTemplateName("template/hello.vm"));
//		System.out.println(lowerCaseFirstCharacter(""));
	}
}
